package com.sh.data.engine.domain.datadev.offline.model.domain;

import com.sh.data.engine.domain.base.model.BaseDomain;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.util.Date;
import java.util.List;

@Data
@SuperBuilder
@NoArgsConstructor
public class OfflineDevSyncDomain extends BaseDomain {

    private Long nodeId;

    private String nodeName;

    private Long projectId;

    private String sourceDsName;

    private String targetDsName;

    /**
     * 任务描述
     */
    private String remark;

    /**
     * 来源数据源类型
     */
    private String sourceDsType;

    /**
     * 来源数据源id
     */
    private Long sourceDsId;

    /**
     * 目标数据源类型
     */
    private String targetDsType;

    /**
     * 目标数据源id
     */
    private Long targetDsId;

    /**
     * ods层级系统
     */
    private String odsName;

    /**
     * 来源表
     */
    private String sourceTable;

    private String sourceSchema;

    private String targetSchema;

    /*目标表*/
    private String targetTable;

    /*目标表表描述*/
    private String targetTableComment;

    /*目标表是否存在*/
    private Integer targetTableIsExisted = 1;

    /*字段映射*/
    private List<FieldMapping> fieldMappings;

    /**
     * 表的增量类型 f-全量 i-增量
     */
    private String tableIncType;

    /**
     * 表的刷新频率
     */
    private String tableFrequency;

    /**
     * 是否有分区 0-否 1-有
     */
    private Integer isParition;

    /**
     * 是否是富文本 0-否 1-有
     */
    private Integer isSupportText;

    /**
     * 分区定义 json
     */
    private List<PartitionField> paritions;

    /**
     * 替换规则 1-写入前清理已有数据 Insert Overwrite 2-写入前保留已有数据 Insert Into 3-同名覆盖 4-同名忽略 5-主键冲突，替换原有数据
     * 6-主键冲突，丢弃新数据 7-key值相同，覆盖 8-key值相同，忽略
     */
    private Integer dataReplaceRuleId;

    /*同步前是否清理 1-是 0-否"*/
    private Integer isTruncate;

    /**
     * 失败记录数超过多少时，任务结束,值为-1时表示不限制
     */
    private Integer errorNumLimit;

    /**
     * 是否是增量 0-否 1-是
     */
    private Integer isInc;

    /**
     * 过滤条件
     */
    private String incFilter;

    private Integer nodeType;

    private Integer taskStatus;
    private Date startTime;
    private Date endTime;

    //    @ApiModelProperty(value = "离线同步读出总数-运维中心使用")
    private String successCount;

    //    @ApiModelProperty(value = "离线同步读写失败总数-运维中心使用")
    private String failCount;

    //    @ApiModelProperty(value = "离线同步任务状态-运维中心使用")
    private Integer jobStatus;

    /**
     * 每次读取数据条数
     */
//    @ApiModelProperty(value = "每次读取数据条数")
    private Integer fetchSize;

    /**
     * 每次写入数据条数
     */
//    @ApiModelProperty(value = "每次写入数据条数")
    private Integer batchSize;

    /**
     * 并发数
     */
//    @ApiModelProperty(value = "并发数")
    private Integer channel;

    /**
     * 切片字段
     */
//    @ApiModelProperty(value = "切片字段")
    private String splitKey;

    private Integer pgText;

    @Data
    public static class FieldMapping {

        /*来源字段名*/
        private String sourceField;

        /*来源字段类型*/
        private String sourceFieldType;

        /*来源字段是否是主键*/
        private Integer isSourceFieldPk;

        /*目标字段名*/
        private String targetField;

        /*目标字段类型*/
        private String targetFieldType;

        /*目标字段是否是主键*/
        private Integer isTargetFieldPk;

        /*目标字段长度*/
        private Integer targetColumnSize;

        /*目标字段小数位长度*/
        private Integer targetDecimalDigits;

        /*目标字段备注*/
        private String targetComment;

        /*日期编码格式*/
        private String dateFormat;
    }

    @Data
    public static class PartitionField {

        /*分区名名称*/
        private String fieldName;

        /*分区值*/
        private String fieldValue;
    }

    //  public static OfflineDevSyncDomain entity2Domain(OfflineDevSyncTask source) {
    //    OfflineDevSyncDomain target = ConvertUtil.copyProperties(source,
    // OfflineDevSyncDomain.class);
    //    List<OfflineDevSyncTask.PartitionField> sourceParitions = source.getParitions();
    //    List<PartitionField> partitions =
    //        ConvertUtil.copyProperties(sourceParitions, PartitionField.class);
    //    target.setParitions(partitions);
    //    OfflineDevSyncTask.TableMapping sourceTableMapping = source.getTableMappings();
    //    ArrayList<OfflineDevSyncTask.TableMapping> sourceTableMappings = Lists.newArrayList(
    //        sourceTableMapping);
    //    if (CollectionUtils.isNotEmpty(sourceTableMappings)) {
    //      List<TableMapping> tableMappings = Lists.newArrayList();
    //      sourceTableMappings.forEach(
    //          tm -> {
    //            TableMapping tableMapping = ConvertUtil.copyProperties(tm, TableMapping.class);
    //            List<OfflineDevSyncTask.FieldMapping> sourceFieldMappings = tm.getFieldMappings();
    //            List<FieldMapping> fieldMappings =
    //                ConvertUtil.copyProperties(sourceFieldMappings, FieldMapping.class);
    //            List<OfflineDevSyncTask.PartitionField> sourcePartitionFields =
    // tm.getParitionFields();
    //            List<PartitionField> partitionFields =
    //                ConvertUtil.copyProperties(sourcePartitionFields, PartitionField.class);
    //            tableMapping.setParitionFields(partitionFields);
    //            tableMapping.setFieldMappings(fieldMappings);
    //            tableMappings.add(tableMapping);
    //          });
    //      target.setTableMappings(tableMappings);
    //    }
    ////    OfflineSyncSchedule offlineSyncScheduleDomain =
    ////        ConvertUtil.copyProperties(sourceSchedule, OfflineSyncSchedule.class);
    ////    target.setSchedule(offlineSyncScheduleDomain);
    //    return target;
    //  }
    //
    //  public static OfflineDevSyncTask domain2Entity(OfflineDevSyncDomain source) {
    //    OfflineDevSyncTask target = ConvertUtil.copyProperties(source, OfflineDevSyncTask.class);
    //    List<PartitionField> sourceParitions = source.getParitions();
    //    List<OfflineDevSyncTask.PartitionField> partitions =
    //        ConvertUtil.copyProperties(sourceParitions, OfflineDevSyncTask.PartitionField.class);
    //    target.setParitions(partitions);
    //
    //    if (CollectionUtils.isNotEmpty(sourceTableMappings)) {
    //      List<OfflineDevSyncTask.TableMapping> tableMappings = Lists.newArrayList();
    //      sourceTableMappings.forEach(
    //          tm -> {
    //            OfflineDevSyncTask.TableMapping tableMapping =
    //                ConvertUtil.copyProperties(tm, OfflineDevSyncTask.TableMapping.class);
    //            List<FieldMapping> sourceFieldMappings = tm.getFieldMappings();
    //            List<OfflineDevSyncTask.FieldMapping> fieldMappings =
    //                ConvertUtil.copyProperties(
    //                    sourceFieldMappings, OfflineDevSyncTask.FieldMapping.class);
    //            List<PartitionField> sourceParitionFields = tm.getParitionFields();
    //            List<OfflineDevSyncTask.PartitionField> partitionFields =
    //                ConvertUtil.copyProperties(
    //                    sourceParitionFields, OfflineDevSyncTask.PartitionField.class);
    //            tableMapping.setParitionFields(partitionFields);
    //            tableMapping.setFieldMappings(fieldMappings);
    //            tableMappings.add(tableMapping);
    //          });
    //      target.setTableMappings(tableMappings.get(0));
    //    }
    //    return target;
    //  }
}
